﻿CREATE PROCEDURE [dbo].[proc_Customer_Getlist_AuditRepeats]
	(
		@CusId nvarchar(50),
		@UserName nvarchar(50)
	)
AS
BEGIN
	DECLARE @CompanyId int
	Declare @CusName nvarchar(50), @Tel nvarchar(50),@MnemonicWorks nvarchar(50)
	Select @CompanyId=CompanyId From Employee Where UserName=@UserName
	Select @CusName=CustomerName,@Tel=Tel,@MnemonicWorks=MnemonicWorks From Customer Where CustomerId=@CusId

	;WITH list As (Select Top 10 ROW_NUMBER() OVER (ORDER BY cus.CreateDate DESC)AS Row,
	cus.Id,
	cus.CustomerId,
	cus.CustomerName,
	cus.MnemonicWorks,
	cus.Tel,
	cus.CreateName,
	cus.CreateDate,
	(Select Title From Customer_Protect Where Id=cus.ProtectId) As Protect,
	(Select EName From Employee Where UserName=cus.CreateName) As CreateNameEName,
	Audit
	From Customer cus Where cus.CompanyId=@CompanyId and (cus.CustomerName=@CusName Or cus.Tel=@Tel Or cus.CustomerName like '%'+@MnemonicWorks+'%') ORDER BY cus.CreateDate DESC
	)
	Select * From list
END	
	RETURN
